from string import upper
import re
from random import *

notenames = { "C":0, "C#":1, "D":2 , "D#":3, "E":4,"F":5,"F#":6, "G":7,"G#":8, "A":9, "A#":10, "B":11 }
notepat = re.compile( "([ABCDEFG]#?)(\d+)")

def note_from_name( note ):
	"""for any given note in the form note octave (for 
	example F4 or c#2 returns the correct midi number"""
	pip = notepat.match( upper(note) )
	if pip:	
		return _convert( pip.group(2) , pip.group(1) )
	else:
		return -1

def _convert( octave , notename ):
	octave = int( octave )
	note = upper(notename)
	if notenames.has_key( note ):
		ret = octave * 12 + notenames[note  ]
	else:
		ret = -1
	return ret
	
def randomNote( ):
	oct = int( random() * 12 )
	note = int( random() * 12 )
	notename = notenames.keys()[ note ]
	return "%s%u" % ( notename , oct )

if __name__ == '__main__':
	pass
